package com.mx.adapt.pager.transformer

import android.view.View
import androidx.viewpager2.widget.ViewPager2
import kotlin.math.abs

/**
 * 切换缩放+alpha变更动画
 * @param scaleMin 缩放最小值
 * @param alphaMin 透明度最小值
 */
class MXScaleTransForm(
    private val scaleMin: Float = 0.8f,
    private val alphaMin: Float = 0.7f
) : ViewPager2.PageTransformer {
    override fun transformPage(page: View, position: Float) {
        val scale = if (position < 0) {
            (1 - scaleMin) * position + 1
        } else {
            (scaleMin - 1) * position + 1
        }
        val alpha = if (position < 0) {
            (1 - alphaMin) * position + 1
        } else {
            (alphaMin - 1) * position + 1
        }
        //为了滑动过程中，page间距不变，这里做了处理
        if (position < 0) {
            page.pivotX = page.width / 2f
            page.pivotY = page.height / 2f
        } else {
            page.pivotX = page.width / 2f
            page.pivotY = page.height / 2f
        }
        page.scaleX = scale
        page.scaleY = scale
        page.alpha = abs(alpha)
    }
}